<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Util
{


   function pagination($total_rows, $current_offset, $items_per_page=ITEMS_PER_PAGE)
   {

      $pages=ceil($total_rows/$items_per_page);
	  $html = '<select id="page_no" name="page_no" onchange="GoToPage(this.value)">';
      for($i=0; $i<$pages; $i++)
	  {
          $offset=$i*$items_per_page;
          $selected=($current_offset<=$offset AND $current_offset>$offset-$items_per_page) ? 'selected="selected"' : "";
		  $html .= '<option value="' . $offset .'" '. $selected .' >' . ($i+1) . '</option>';
	  }
	  $html .= '</select>';
	  return $html;
   }
   
	function handle_error($sql=true)
    {
    	if(!defined("DEBUG"))
			define("DEBUG", true); 

		if(DEBUG)
		{
			$CI =& get_instance();
			return "error:" . mysql_error() . "<br/>" . $CI->db->last_query() . "<br/>Error number:" . mysql_errno();
		}
		else
		{	
			return  "Generic database error: " . mysql_errno();
		}
	}

	function send_json($data)
	{
		$CI =& get_instance();
		if(($CI->input->post("MAX_FILE_SIZE"))){
           echo("<textarea>".json_encode($data)."</textarea>");
		}
		else {
             echo json_encode($data);
		}

	}

	function send_email($email, $message, $subject = "Newsletter")
    {
    	$this->CI = &get_instance();

   		 $this->CI->load->library('email');
	       $config['mailtype']="text";
		   $config['smtp_host']='smtp.alwaysdata.com';
		   $config['smtp_user']='petea_ro@alwaysdata.net';
	       $config['smtp_pass']='qwe123';
		   $config['smtp_port']="25";
		   $config['protocol']="smtp";
		   $config['smtp_timeout']="10";

		   $this->CI->email->initialize($config);
		   $this->CI->email->from("norespond@sevenenergy.com");
		   $this->CI->email->bcc($email);
           // $this->CI->email->to($email);

		   $this->CI->email->subject($subject);
		   $this->CI->email->message($message);

		 if(!$this->CI->email->send()) {
		 	$data['error']=$this->CI->email->print_debugger();
		 }
		 else{
		    $data['success'] = "An email has been send to $email address, please check mail box";
		 }

	return $data;
    }

}
